﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO = Viewer.BusinessObject.BusinessEntities;
using Ext.Net;
using System.Collections;
using Viewer.BusinessObject.BusinessEntities;
namespace Viewer.ToChuc.UserControls
{
    public partial class UcAddToChuc : System.Web.UI.UserControl
    {
        BO.NSToChuc tochuc = new Viewer.BusinessObject.BusinessEntities.NSToChuc();
        BO.CommonCode common = new Viewer.BusinessObject.BusinessEntities.CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadDataForControl();
            }
        }
        void LoadDataForControl()
        {
            //Load Tổ chức
            storeTochuc.DataSource = tochuc.LoadAllToChuc();
            storeTochuc.DataBind();
            //Load xếp hạng
            storeXepHang.DataSource = common.LoadDataCommonByCodeKey(common.XepHangDonVi);
            storeXepHang.DataBind();
            //Load Khoi
            storeKhoi.DataSource = common.LoadDataCommonByCodeKey(common.KhoiQuanlyToChuc);
            storeKhoi.DataBind();
            //Loai To Chuc
            storeLoaiToChuc.DataSource = common.LoadDataCommonByCodeKey(common.LoaiToChuc);
            storeLoaiToChuc.DataBind();
        }
        void LoadDataView(int iD)
        {

            NSToChuc oNSToChuc = (new NSToChuc()).GetByID(iD);
            txtId.Text = oNSToChuc.ToChucID.ToString();
            txtShortName.Text = oNSToChuc.TenToChucShort.ToString();
            txtFullName.Text = oNSToChuc.TenToChucFull.ToString();
            txtDiaChi.Text = oNSToChuc.DiaChi;
            txtGhiChu.Text = oNSToChuc.GhiChu;
            cbbKhoi.SelectedItem.Value = oNSToChuc.KhoiQuanLyID.ToString();
            cbbXepHang.SelectedItem.Value = oNSToChuc.XepHangDonViID.ToString();
            ccbToChucCha.SelectedItem.Value = oNSToChuc.ToChucIDCha.ToString();
            hdDiaChiId.Value = oNSToChuc.DiaChiID.ToString();
            txtToChucHanhChinhTrucThuoc.Text = oNSToChuc.TenToChucHanhChinhTrucThuoc;
            hdToChucHanhChinhTruocThuocID.Value = oNSToChuc.DonViHanhChinhTrucThuocID.ToString();
            cboLoaiToChuc.SelectedItem.Value = oNSToChuc.LoaiToChucID.ToString();

        }
        public void ShowAddNewDialog(string type, int id, int idToChucCha)
        {
            //AddNew
            wdAddNewToChuc.Show();
            if (type.Equals("AddNew"))
            {
                ResetControl();
                this.wdAddNewToChuc.Title = "Tạo mới tổ chức";
                this.hdStatus.Value = "AddNew";

                this.btnSave.Hidden = false;
                this.cbbKhoi.ReadOnly = false;
                this.cbbXepHang.ReadOnly = false;
                this.txtGhiChu.ReadOnly = false;
                this.txtFullName.ReadOnly = false;
                this.txtShortName.ReadOnly = false;
                this.btnLookUp.Hidden = false;
                this.ccbToChucCha.SelectedItem.Value = idToChucCha > 0 ? idToChucCha.ToString() : "Không có tổ chức cha";
                this.cboLoaiToChuc.ReadOnly = false;
                this.btnLookUpToChucHanhChinhTrucThuoc.Hidden = false;
            }
            else if (type.Equals("View"))
            {
                this.wdAddNewToChuc.Title = "Chi tiết tổ chức";
                this.btnSave.Hidden = true;
                this.cbbKhoi.ReadOnly = true;
                this.cbbXepHang.ReadOnly = true;
                this.ccbToChucCha.ReadOnly = true;
                this.txtGhiChu.ReadOnly = true;
                this.txtFullName.ReadOnly = true;
                this.txtShortName.ReadOnly = true;
                this.btnLookUp.Hidden = true;
                this.cboLoaiToChuc.ReadOnly = true;
                this.btnLookUpToChucHanhChinhTrucThuoc.Hidden = true;
                LoadDataView(id);
            }
            else if (type.Equals("Edit"))
            {
                hdStatus.Value = "Edit";

                btnSave.Hidden = false;
                cbbKhoi.ReadOnly = false;
                cbbXepHang.ReadOnly = false;
                txtGhiChu.ReadOnly = false;
                txtFullName.ReadOnly = false;
                txtShortName.ReadOnly = false;
                btnLookUp.Hidden = false;
                cboLoaiToChuc.ReadOnly = false;
                wdAddNewToChuc.Title = "Sửa tổ chức";
                ccbToChucCha.ReadOnly = true;
                btnLookUpToChucHanhChinhTrucThuoc.Hidden = false;
                LoadDataView(id);
            }
        }
        void ResetControl()
        {
            txtToChucHanhChinhTrucThuoc.Text = string.Empty;
            txtDiaChi.Text = string.Empty;
            txtFullName.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
            txtShortName.Text = string.Empty;
            cbbKhoi.Text = string.Empty;
            cbbXepHang.Text = string.Empty;
            ccbToChucCha.Text = string.Empty;
            hdToChucHanhChinhTruocThuocID.Text = string.Empty;
            hdDiaChiId.Text = string.Empty;
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            int idCha;
            if (hdIdCha.Value.ToString().Trim() == string.Empty || hdIdCha.Value.ToString().Trim() == "Không có tổ chức cha")
            {
                idCha = 0;
            }
            else idCha = Convert.ToInt16(hdIdCha.Value);

            if (hdStatus.Value.ToString() == "AddNew")
            {

                tochuc.InsertToChuc(txtShortName.Text, txtFullName.Text, idCha, 2, Convert.ToInt16(cbbXepHang.SelectedItem.Value), Convert.ToInt16(cbbKhoi.SelectedItem.Value), Convert.ToInt16(hdDiaChiId.Value), txtGhiChu.Text, int.Parse(hdToChucHanhChinhTruocThuocID.Text), int.Parse(cboLoaiToChuc.SelectedItem.Value));

            }
            else
            {
                tochuc.UpdateTochuc(Convert.ToInt16(txtId.Text), txtShortName.Text, txtFullName.Text,
                    Convert.ToInt16(hdKhoi.Value), Convert.ToInt16(hdXepHang.Value), txtGhiChu.Text,
                    Convert.ToInt16(hdDiaChiId.Value), int.Parse(hdToChucHanhChinhTruocThuocID.Text),
                    int.Parse(cboLoaiToChuc.SelectedItem.Value));

            }
            ((PhanQuyenTheoToChuc)this.Page).LoadDataForTree();
            ((PhanQuyenTheoToChuc)this.Page).LoadGrid(idCha.ToString(),"");
            wdAddNewToChuc.Hide();
        }
        protected void btnLookUp_Click(object sender, DirectEventArgs e)
        {
            UCLookUpDiaChi.ShowDialogDiaChi();
        }

        protected void btnLookUpToChucHanhChinhTrucThuoc_Click(object sender, DirectEventArgs e)
        {
           // UCLookUpToChucHanhChinh1.();
        }
    }
}